package com.lzb.newloan.veticalscrolltext;

import java.util.ArrayList;
import java.util.List;

import android.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.TextView;
import com.lzb.newloan.pojo.PersonPojo;


public class VerticalScrollTextView extends TextView
{
    private static final int DY = 60;
    public int index;
    private boolean isFirstStart;
    public boolean isStart;
    private List<PersonPojo> list;
    Handler mHandler;
    private Paint mPaint;
    private Paint mPathPaint;
    public float mTouchHistoryY;
    Runnable mUpdateResults;
    private float mX;
    private int mY;
    private float middleY;
    private float txtSize;
    
    public VerticalScrollTextView(final Context context) {
        super(context);
        this.index = 0;
        this.isStart = false;
        this.isFirstStart = true;
        this.mHandler = new Handler();
        this.mUpdateResults = new Runnable() {
            @Override
            public void run() {
                VerticalScrollTextView.this.invalidate();
            }
        };
        this.init();
    }
    
    public VerticalScrollTextView(final Context context, final AttributeSet set) {
        super(context, set);
        this.index = 0;
        this.isStart = false;
        this.isFirstStart = true;
        this.mHandler = new Handler();
        this.mUpdateResults = new Runnable() {
            @Override
            public void run() {
                VerticalScrollTextView.this.invalidate();
            }
        };
        this.init();
    }
    
    public VerticalScrollTextView(final Context context, final AttributeSet set, final int n) {
        super(context, set, n);
        this.index = 0;
        this.isStart = false;
        this.isFirstStart = true;
        this.mHandler = new Handler();
        this.mUpdateResults = new Runnable() {
            @Override
            public void run() {
                VerticalScrollTextView.this.invalidate();
            }
        };
        this.init();
    }
    
    private void init() {
        this.setFocusable(true);
        if (this.list == null) {
            (this.list = new ArrayList<PersonPojo>()).add(new PersonPojo("2012-3-24", "发发呆"));
        }
        (this.mPaint = new Paint()).setAntiAlias(true);
        this.mPaint.setTextSize(14f);
        this.mPaint.setColor(this.getResources().getColor(R.color.black));
        this.mPaint.setTypeface(Typeface.SERIF);
        (this.mPathPaint = new Paint()).setAntiAlias(true);
        this.mPathPaint.setColor(this.getResources().getColor(R.color.black));
        this.mPathPaint.setTextSize(25.0f);
        this.mPathPaint.setTypeface(Typeface.SERIF);
    }
    
    public List<PersonPojo> getList() {
        return this.list;
    }
    
    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(251592703);
        final Paint mPaint = this.mPaint;
        final Paint mPathPaint = this.mPathPaint;
        mPaint.setTextAlign(Paint.Align.LEFT);
        if (this.index != -1) {
            mPathPaint.setTextAlign(Paint.Align.LEFT);
            canvas.drawText(this.list.get(this.index).getCardusername(), this.mX, this.middleY, mPathPaint);
            float middleY = this.middleY;
            for (int i = this.index - 1; i >= 0; --i) {
                middleY -= 60.0f;
                if (middleY < 0.0f) {
                    break;
                }
                canvas.drawText(this.list.get(i).getCardusername(), this.mX, middleY, mPaint);
            }
            float middleY2 = this.middleY;
            for (int j = this.index + 1; j < this.list.size(); ++j) {
                middleY2 += 60.0f;
                if (middleY2 > this.mY) {
                    break;
                }
                canvas.drawText(this.list.get(j).getCardusername(), this.mX, middleY2, mPaint);
            }
        }
    }
    
    protected void onSizeChanged(final int n, final int my, final int n2, final int n3) {
        super.onSizeChanged(n, my, n2, n3);
        this.mX = n * 0.5f;
        this.mY = my;
        this.middleY = my * 0.5f;
    }
    
    public void setList(final List<PersonPojo> list, final float txtSize) {
        this.txtSize = txtSize;
        (this.mPaint = new Paint()).setAntiAlias(true);
        this.mPaint.setTextSize(this.txtSize * 12.0f);
        this.mPaint.setColor(this.getResources().getColor(R.color.black));
        this.mPaint.setTypeface(Typeface.SERIF);
        (this.mPathPaint = new Paint()).setAntiAlias(true);
        this.mPathPaint.setColor(this.getResources().getColor(R.color.black));
        this.mPathPaint.setTextSize(this.txtSize * 12.0f);
        this.mPathPaint.setTypeface(Typeface.SERIF);
        this.isStart = true;
        this.list = list;
    }
    
    public long updateIndex(final int index) {
        if (index == -1) {
            return -1L;
        }
        this.index = index;
        return index;
    }
    
    public void updateUI() {
        if (this.isFirstStart) {
            new Thread(new updateThread()).start();
            this.isFirstStart = false;
        }
    }
    
    class updateThread implements Runnable
    {
        int i;
        long time;
        
        updateThread() {
            this.time = 1000L;
            this.i = 0;
        }
        
        @Override
        public void run() {
            while (VerticalScrollTextView.this.isStart) {
                final long updateIndex = VerticalScrollTextView.this.updateIndex(this.i);
                this.time += updateIndex;
                VerticalScrollTextView.this.mHandler.post(VerticalScrollTextView.this.mUpdateResults);
                if (updateIndex == -1L) {
                    break;
                }
                try {
                    Thread.sleep(this.time);
                    ++this.i;
                    if (this.i != VerticalScrollTextView.this.getList().size()) {
                        continue;
                    }
                    this.i = 0;
                }
                catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}
